package lzupdater.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;

import lzupdater.LZRosterUpdater;
import lzupdater.com.CommunicationHub;
import lzupdater.com.HubComponent;
import lzupdater.locale.Dictionary;
import lzupdater.locale.Locale;
import lzupdater.locale.LocaleListener;
import lzupdater.locale.LocalizedString;
import lzupdater.utils.LZULogger;
import lzupdater.utils.LZUPreferences;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

public class MainGui extends HubComponent implements WindowListener,
		LocaleListener {

	private final static LocalizedString quittingLZUpdater = new LocalizedString("Quitting LZUpdater");
//	private final static LocalizedString profilesTitle = new LocalizedString("Profiles");
//	private final static LocalizedString preferencesTitle = new LocalizedString("Preferences");
	private static final LocalizedString from = new LocalizedString("from");
	private static final LocalizedString by = new LocalizedString("by");

	private MenuBar                   menuBar;
	private ProfilePanel         profilePanel;
	private ProfileListPanel profileListPanel;
	private LoggerPanel           loggerPanel;

	private LZULogger logger;
	private LZUPreferences preferences;

	private JFrame     frame;

	private void setTitle() {
		frame.setTitle(String.format("LZRosterUpdater (v%s) %s %s %s %s",
				LZRosterUpdater.VERSION, by, LZRosterUpdater.AuthorName, from,
				LZRosterUpdater.AuthorServer));
	}

	/**
	 * Constructor. Instanciate the component of the gui.
	 * 
	 * @param hub
	 */
	public MainGui(CommunicationHub hub) {
		super(hub);

		// Main frame
		frame = new JFrame();

		// Create and register the sub-component to the hub
		new ProfileListPanel(hub);
		new ProfilePanel(hub);
		new LoggerPanel(hub);
		new PreferencesPanel(hub);
		new MenuBar(hub);
		new AboutFrame(hub);
		new DebugPanel(hub);

		Dictionary.addLocaleListener(this);
		setTitle();
	}

	/**
	 * Launch the GUI, i.e. make it visible
	 */
	public void launch() {
		frame.pack();
		frame.setVisible(true);
	}

	/**
	 * {@inheritDoc}
	 */
	public String getKey() {
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public void localeChange(Locale locale) {
		setTitle();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void initializeCommunication() {
		logger = hub.getComponent(LZULogger.class);
		preferences = hub.getComponent(LZUPreferences.class);
		loggerPanel = hub.getComponent(LoggerPanel.class);
		profilePanel = hub.getComponent(ProfilePanel.class);
		profileListPanel = hub.getComponent(ProfileListPanel.class);
		menuBar = hub.getComponent(MenuBar.class);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void initialize() {
		initialiseGui();
	}

	public JFrame getGui() {
		return this.frame;
	}
	
	private void initialiseGui() {

		// Profiles panel
		JSplitPane jSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true,
				initialiseTopPanel(), initialiseBottomPanel());


		// finishing GUI initialization
		frame.setJMenuBar(menuBar.getGui());
		frame.setContentPane(jSplit);
		frame.addWindowListener(this);
		frame.setPreferredSize(new Dimension(840, 600));
		frame.setSize(frame.getPreferredSize());
		frame.setMinimumSize(frame.getPreferredSize());

	}
	
	private Component initialiseTopPanel() {
		return new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
				profileListPanel.getGui(), profilePanel.getGui());
	}

	private Component initialiseBottomPanel() {
		return loggerPanel.getGui();
	}

	//
	// WindowListener interface
	//

	public void windowClosing(WindowEvent e) {
		// warn the profile panel that the window is closing.
		profilePanel.windowIsClosing();
		try {
			logger.info(quittingLZUpdater);
			preferences.save();
		} catch (IOException ex) {
			//
		}

		System.exit(0);
	}

	public void windowActivated(WindowEvent e) {
	}

	public void windowClosed(WindowEvent e) {
	}

	public void windowDeactivated(WindowEvent e) {
	}

	public void windowDeiconified(WindowEvent e) {
	}

	public void windowIconified(WindowEvent e) {
	}

	public void windowOpened(WindowEvent e) {
	}

	private JDialog dialog = null;
	
	public void displayAbout() {
		if (dialog == null) {
			JOptionPane.showMessageDialog(this.frame, "Hello");
		}
	}
	
	
}
